<template>
  <div>
    <div class="form-content" v-if="statusDrawing" :key="1">
      <p>2.0负责图纸修改</p>
      <el-form ref="formDraw" class="form-input" label-width="140px" :model="formDrawData" :rules="ruleDrawData">
        <el-row>
          <el-col :span="12">
            <el-form-item label="上传CAD格式文件">
              <UploadFile @getfiles="getfiles1" :showFiles="showFiles1"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="自检是否完成">
              <el-switch v-model="formDrawData.complete1"></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="上传PDF格式文件">
              <UploadFile @getfiles="getfiles2" :showFiles="showFiles2"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="自检是否完成">
              <el-switch v-model="formDrawData.complete2"></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="备注" prop="remarks">
            <el-input type="textarea" v-model="formDrawData.remarks"></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="下一流程审核人" prop="nextAuthor">
            <el-select placeholder="请选择审核人" v-model="formDrawData.nextAuthor" multiple @change="handleChange">
              <el-option v-for="item in nextAuthorLists" :key="item.id" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <div class="btn-option">
          <el-button type="primary" @click="submitDraw">提交到下一流程</el-button>
          <el-button @click="cancel">取消</el-button>
          <el-button type="warning" @click="back">退回到上一流程</el-button>
        </div>
      </el-form>
    </div>

    <div class="form-content" v-else :key="2">
      <p>2.0负责图纸修改</p>
      <el-form class="form-input" label-width="140px" :model="formDrawData2">
        <el-row>
          <el-col :span="12">
            <el-form-item label="下载CAD格式文件">
              <div v-if="formDrawData2.file1.length > 0">
                <div v-for="item in formDrawData2.file1" :key="item.id" @click="downFile1(item.id)" class="filediv">{{item.fileName}}</div>
              </div>
              <div v-else>未上传文件</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="自检是否完成">
              <el-switch v-model="formDrawData2.complete1" disabled></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="下载PDF格式文件">
              <div v-if="formDrawData2.file2.length > 0">
                <div v-for="item in formDrawData2.file2" :key="item.id" @click="downFile2(item.id)" class="filediv">{{item.fileName}}</div>
              </div>
              <div v-else>未上传文件</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="自检是否完成">
              <el-switch v-model="formDrawData2.complete2" disabled></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="备注">
            <el-input type="textarea" v-model="formDrawData2.remarks" readonly></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="下一流程审核人">
              <el-input v-model="formDrawData2.nextAuthor" readonly></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>
<script>
import { EmbodimentCreate, getAuditFlowDetails, getAuthorByNodeID, downloadFile } from '@/api/api'
import { nextNodeArr, getAccount, getMultipleLabel, getFileId, getAccountBack } from '@/unils/utils'
import swal from 'sweetalert'
import _ from 'underscore'
import Bus from '@/plugins/bus'
import uploadFile from '../flow/upload'
export default {
  components: {
    UploadFile: uploadFile,
  },
  props: {
    drawingId: {
      type: String,
      defalut: ''
    },
    nodeIdId: {
      type: [String, Number],
      defalut: 2
    },
    statusDrawing: {
      type: Boolean,
      defalut: true
    }
  },
  data () {
    return {
      formDrawData: {
        complete1: false,
        complete2: false,
        remarks: '',
        nextAuthor: [],
        file1: [],
        file2: []
      },
      labelName: '',
      nextAuthorLists: [],
      ruleDrawData: {
        nextAuthor: [{ required: true, message: '请选择审核人!', trigger: 'change' }],
        remarks: [{ required: true, message: '请填写备注', trigger: 'blur' }]
      },
      formDrawData2: {
        complete1: false,
        complete2: false,
        remarks: '',
        nextAuthor: '',
        addName: '',
        file1: [],
        file2: []
      },
      backNextAuthorCN: '',
      backNextAuthor: '',
      //
      showFiles1: [],
      showFiles2: [],
      fileList2_1: [],
      fileList2_2: []
    }
  },
  mounted () {
    // console.log(this.nodeIdId)
    this.getDatas()
    // 打印节点不调用
    if (this.nodeIdId < 12) {
      this.getNodeName()
    }
  },
  methods: {
    getDatas () {
      let params = {
        orunid: this.drawingId
      }
      getAuditFlowDetails(params).then(res => {
        // console.log(res)
        if (res.code == 200) {
          this.formDrawData2.complete1 = JSON.parse(res.data.radio3)
          this.formDrawData2.complete2 = JSON.parse(res.data.radio4)
          this.formDrawData2.remarks = res.data.remarks3
          if (!this.statusDrawing) {
            // 判断是不是回退到'T10002'，如果是，则不赋值
            let arr = JSON.parse(res.data.nodeTowardsInfo)
            let obj = {}
            obj = arr.find(item => {
              return item.currentNodeID === 'T10003'
            })
            if (obj.nextNodeID === 'T10002') {
              this.formDrawData2.nextAuthor = ''
            } else {
              this.formDrawData2.nextAuthor = obj.nextAuthor
            }
          }
          this.formDrawData2.addName = res.data.addName
          if (res.data.cadfile) {
            this.formDrawData2.file1 = JSON.parse(res.data.cadfile)
            // 上传文件封装传值处理
            this.showFiles1 = JSON.parse(res.data.cadfile)
            // 保留原文件作为提交
            this.fileList2_1 = JSON.parse(res.data.cadfile)
          }
          if (res.data.pdffile) {
            this.formDrawData2.file2 = JSON.parse(res.data.pdffile)
            this.showFiles2 = JSON.parse(res.data.pdffile)
            this.fileList2_2 = JSON.parse(res.data.pdffile)
          }
          Bus.$emit('res', res)
          // 获取要退回的域账号
          this.backNextAuthor = getAccountBack(res.data.nodeTowardsInfo, 'T10002')
          // 被驳回时显示上次提交数据
          // this.formDrawData.complete1 = Boolean(res.data.radio3)
          // this.formDrawData.complete2 = Boolean(res.data.radio4)
          // this.formDrawData.remarks = res.data.remarks3
        } else {
          swal('错误', res.message || '网络连接错误', 'warning')
        }
      }).catch((err) => {
        swal('错误', '获取数据失败', 'error')
      })
    },
    handleChange (val) {
      // this.labelName = getMultipleLabel(val, this.nextAuthorLists)
    },
    // 文件操作
    getfiles1 (e) {
      // 包含原、新文件，筛选新文件提交
      this.formDrawData.file1 = e[0]
      // 用于原文件提交
      this.fileList2_1 = e[1]
    },
    getfiles2 (e) {
      this.formDrawData.file2 = e[0]
      this.fileList2_2 = e[1]
    },
    downFile1 (id) {
      downloadFile({ id: id }).then(res => {
        if (res.code == 200) {
          window.open(res.data.url, '_self')
        }
      }).catch(e => {
        console.log(e)
      })
    },
    downFile2 (id) {
      downloadFile({ id: id }).then(res => {
        if (res.code == 200) {
          window.open(res.data.url, '_self')
        }
      }).catch(e => {
        console.log(e)
      })
    },
    submitDraw: _.debounce(async function () {
      await this.$refs.formDraw.validate(async (valid) => {
        if (!valid) return false
        let msg = '是否确认提交!'
        // if (this.formDrawData.file1.length == 0 || this.formDrawData.file2.length == 0) {
        //   msg = '检测到有文件没提交，是否确认提交!'
        // }
        await this.confirm(msg, async () => {
          let file1Obj = await getFileId(this, this.formDrawData.file1, 1, 2)
          let file2Obj = await getFileId(this, this.formDrawData.file2, 1, 2)
          let params = {
            radio3: this.formDrawData.complete1,
            radio4: this.formDrawData.complete2,
            remarks3: this.formDrawData.remarks,
            orunid: this.drawingId,
            status: 'current',
            type: 2,
            addName: this.formDrawData2.addName,
            currentNodeName: '2.0负责图纸修改',
            currentNodeID: 'T10003',
            nextNodeID: 'T10004',
            nodeTowardsInfo: nextNodeArr(this.$store.state.jobAccount, this.formDrawData.nextAuthor, 'T10004', '3.0负责图纸审核', 'T10003', '2.0负责图纸修改'),
            cadfile: JSON.stringify(file1Obj.concat(this.fileList2_1)),
            pdffile: JSON.stringify(file2Obj.concat(this.fileList2_2))
          }
          // console.log(JSON.stringify(file1Obj.concat(this.fileList2_1)))
          // console.log(JSON.stringify(file2Obj.concat(this.fileList2_2)))
          EmbodimentCreate(params)
            .then((res) => {
              // console.log(res)
              if (res.code == 200) {
                this.alertok('提交成功', () => {
                  this.$router.go(-1)
                })
              } else {
                swal('错误', res.message || '网络连接错误', 'warning')
              }
            })
            .catch((err) => {
              swal('错误', '网络连接错误', 'error')
            })
        })
      })
    }, 800),
    cancel () {
      this.$router.go(-1)
    },
    async back () {
      if (!this.formDrawData.remarks) {
        swal('错误', '请在备注填写退回原因！', 'warning')
        return
      }
      await this.confirm('是否确认退回', async () => {
        let file1Obj = await getFileId(this, this.formDrawData.file1, 1, 2)
        let file2Obj = await getFileId(this, this.formDrawData.file2, 1, 2)
        let params = {
          radio3: this.formDrawData.complete1,
          radio4: this.formDrawData.complete2,
          remarks3: this.formDrawData.remarks,
          orunid: this.drawingId,
          status: 'current',
          type: 0,
          addName: this.formDrawData2.addName,
          currentNodeName: '2.0负责图纸修改',
          currentNodeID: 'T10003',
          nextNodeID: 'T10002',
          nodeTowardsInfo: nextNodeArr(this.$store.state.jobAccount, [this.backNextAuthor], 'T10002', '1.0设计方案原资料', 'T10003', '2.0负责图纸修改'),
          cadfile: JSON.stringify(file1Obj),
          pdffile: JSON.stringify(file2Obj)
        }
        // console.log(params)
        EmbodimentCreate(params)
          .then((res) => {
            if (res.code == 200) {
              this.alertok('回退成功', () => {
                this.$router.go(-1)
              })
            } else {
              swal('错误', res.message || '网络连接错误', 'warning')
            }
          })
          .catch((err) => {
            swal('错误', '网络连接错误', 'error')
          })
      })
    },
    // 获取人员
    getNodeName () {
      let params = {}
      if (this.nodeIdId == 2) {
        return
      }
      // 图纸
      if (this.nodeIdId == 3) {
        params = {
          nodeID: 'T10004'
        }
      }
      // 审核
      if (this.nodeIdId == 4) {
        params = {
          nodeID: 'T10005'
        }
      }
      // 预算
      if (this.nodeIdId == 5) {
        params = {
          nodeID: 'T10006'
        }
      }
      // 参数表
      if (this.nodeIdId == 6) {
        params = {
          nodeID: 'T10007'
        }
      }
      // 实施方案
      if (this.nodeIdId == 7) {
        params = {
          nodeID: 'T10007'
        }
      }
      // 导PDF
      if (this.nodeIdId == 8) {
        params = {
          nodeID: 'T10011'
        }
      }
      // 导PDF
      if (this.nodeIdId == 9) {
        params = {
          nodeID: 'T10011'
        }
      }
      // 导PDF
      if (this.nodeIdId == 10) {
        params = {
          nodeID: 'T10011'
        }
      }
      // 合并完整方案
      if (this.nodeIdId == 11) {
        params = {
          nodeID: 'T10012'
        }
      }
      // console.log(params)
      getAuthorByNodeID(params).then(res => {
        // console.log(res)
        if (res.code == 200) {
          this.nextAuthorLists = res.data
          Bus.$emit('resAuthor', res.data)
        }
      }).catch((err) => {
        console.log(err)
      })
    }
  }
}
</script>
<style lang="less" scoped>
.form-content {
  p {
    text-align: center;
    font-size: 30px;
    margin-bottom: 40px;
  }
  .form-input {
    width: 70%;
    // background-color: blue;
    margin: auto;
    .btn-option {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>